package controllers.vecino;

import java.util.Collection;
import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ComunidadDeVecinosService;
import services.ReunionService;
import services.VecinoService;
import controllers.AbstractController;
import domain.ComunidadDeVecinos;
import domain.Reunion;

@Controller
@RequestMapping("/reunion/vecino")
public class ReunionVecinoController extends AbstractController {

	@Autowired
	private ReunionService reunionService;

	@Autowired
	private VecinoService vecinoService;
	@Autowired
	private ComunidadDeVecinosService comunidadDeVecinosService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int comunidadId) {
		ModelAndView result;
		Collection<Reunion> reuniones;
		Boolean isPresident;

		reuniones = reunionService.findByCommunityVecino(comunidadId);
		isPresident = comunidadDeVecinosService.isPresidente(comunidadId);

		result = new ModelAndView("reunion/list");
		result.addObject("requestURI", "reunion/vecino/list.do");
		result.addObject("comunidadId", comunidadId);
		result.addObject("reuniones", reuniones);
		result.addObject("isPresident", isPresident);
		result.addObject("backURI", "/comunidad/vecino/edit.do?comunidadId="
				+ comunidadId);

		return result;
	}

	// Creation
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create(@RequestParam int comunidadId) {
		ModelAndView result;
		Reunion reunion;
		ComunidadDeVecinos cv;
		Boolean isPresident;
		Boolean isNew;

		isPresident = comunidadDeVecinosService.isPresidente(comunidadId);
		cv = comunidadDeVecinosService.findOne(comunidadId);
		reunion = reunionService.create(cv);
		isNew = true;

		result = new ModelAndView("reunion/create");
		result.addObject("isPresident", isPresident);
		result.addObject("reunion", reunion);
		result.addObject("requestURI", "reunion/vecino/edit.do");
		result.addObject("backURI", "/comunidad/vecino/edit.do?comunidadId="
				+ comunidadId);
		result.addObject("isNew", isNew);

		return result;

	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int reunionId) {
		ModelAndView result;
		Reunion reunion;
		Boolean noCelebradaAun;
		Boolean join;
		Boolean isNew;
		int comunidadId;
		Boolean isPresident;
		Boolean hasMorosos;
		Boolean hasActa;

		reunion = reunionService.findOne(reunionId);
		hasActa = !(reunion.getActas() == (null) || reunion.getActas()
				.isEmpty());
		join = reunionService.isJoined(reunionId);
		noCelebradaAun = reunion.getFechaCelebracion().after(new Date());
		hasMorosos = vecinoService.findByMorososByComunidad(
				reunion.getComunidadDeVecinos().getId()).size() > 0;

		isNew = false;
		comunidadId = reunion.getComunidadDeVecinos().getId();
		isPresident = comunidadDeVecinosService.isPresidente(comunidadId);

		result = new ModelAndView("reunion/details");
		result.addObject("requestURI", "reunion/edit.do");
		result.addObject("reunionId", reunionId);
		result.addObject("isNew", isNew);
		result.addObject("hasMorosos", hasMorosos);
		result.addObject("hasActa", hasActa);
		result.addObject("isPresident", isPresident);
		result.addObject("reunion", reunion);
		result.addObject("join", join);
		result.addObject("noCelebradaAun", noCelebradaAun);
		result.addObject("backURI", "/reunion/vecino/list.do?comunidadId="
				+ comunidadId);

		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Reunion reunion, BindingResult binding) {
		ModelAndView result;
		Boolean isNew;

		if (binding.hasErrors()) {
			result = createEditModelAndView(reunion);
		} else {
			try {
				reunionService.save(reunion);
				result = new ModelAndView(
						"redirect:/comunidad/vecino/edit.do?comunidadId="
								+ reunion.getComunidadDeVecinos().getId());

			} catch (Throwable oops) {

				result = createEditModelAndView(reunion, "reunion.commit.error");
			}
		}

		isNew = reunion.getId() == 0;
		result.addObject("requestURI", "reunion/vecino/edit.do");
		result.addObject("backURI", "/comunidad/vecino/edit.do?comunidadId="
				+ reunion.getComunidadDeVecinos().getId());
		result.addObject("isNew", isNew);

		return result;
	}

	@RequestMapping(value = "/join", method = RequestMethod.GET)
	public ModelAndView join(@RequestParam int reunionId) {
		ModelAndView result;

		try {
			reunionService.join(reunionId);
			result = new ModelAndView(
					"redirect:/reunion/vecino/edit.do?reunionId=" + reunionId);

		} catch (Throwable oops) {
			Reunion reunion;
			Boolean noCelebradaAun;
			Boolean join;
			Boolean isNew;
			int comunidadId;
			Boolean isPresident;
			Boolean hasMorosos;
			Boolean hasActa;

			reunion = reunionService.findOne(reunionId);
			hasActa = !(reunion.getActas() == (null) || reunion.getActas()
					.isEmpty());

			join = reunionService.isJoined(reunionId);
			noCelebradaAun = reunion.getFechaCelebracion().after(new Date());
			hasMorosos = vecinoService.findByMorososByComunidad(
					reunion.getComunidadDeVecinos().getId()).size() > 0;

			isNew = false;
			comunidadId = reunion.getComunidadDeVecinos().getId();
			isPresident = comunidadDeVecinosService.isPresidente(comunidadId);

			result = new ModelAndView("reunion/details");
			result.addObject("requestURI", "reunion/edit.do");
			result.addObject("reunionId", reunionId);
			result.addObject("isNew", isNew);
			result.addObject("hasMorosos", hasMorosos);
			result.addObject("hasActa", hasActa);
			result.addObject("isPresident", isPresident);
			result.addObject("reunion", reunion);
			result.addObject("join", join);
			result.addObject("noCelebradaAun", noCelebradaAun);
			result.addObject("backURI", "/reunion/vecino/list.do?comunidadId="
					+ comunidadId);

			result.addObject("message", "reunion.commit.error");
			return result;

		}

		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(Reunion reunion) {
		ModelAndView result;

		result = createEditModelAndView(reunion, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Reunion reunion,
			String message) {
		ModelAndView result;

		result = new ModelAndView("reunion/create");
		int comunidadId;
		Boolean isPresident;
		Boolean isNew;

		comunidadId = reunion.getComunidadDeVecinos().getId();
		isPresident = comunidadDeVecinosService.isPresidente(comunidadId);
		isNew = true;

		result.addObject("isPresident", isPresident);
		result.addObject("reunion", reunion);
		result.addObject("requestURI", "reunion/vecino/edit.do");
		result.addObject("backURI", "/comunidad/vecino/edit.do?comunidadId="
				+ comunidadId);
		result.addObject("isNew", isNew);

		result.addObject("message", message);

		return result;
	}

}
